<?php
/**
 * 短信网关调用接口
 * 吴晓峰 2011/02/27
 */
class sms
{
    private $url_sms ="http://www.jitmarketing.cn/SendSMS.asp?code=jit2010sms"; // 短信网关地址
    private $text_sms = ""; //短信内容
    private $user_id; //发送短信的用户ID
    private $tite_sms = ""; //短信标题

    /*
    * 设置短信网关的URL
    */
    public function set_UrlSms($url_sms)
    {
        $this->url_sms = $url_sms;
    }

    /**
     *构造函数
     * @param <type> $text_sms 短信内容
     * @param <type> $user_id  发送短信的用户
     * 
     */
    public function __construct($text_sms, $user_id)
    {
        if (!empty($user_id)) {
            $sql_user = 'select username from users where id=' . $user_id;
            $db2 = &$GLOBALS['db2'];
            $db2->query($sql_user);
            if ($obj = $db2->fetch_object()) {
                $user_name = $obj->username;
                $this->text_sms = $text_sms . '-发自' . $user_name;
            }
            $this->user_id = $user_id;
        }
    }


    public function password($text_sms, $moble_numbers, $userid)
    {
        if (!empty($moble_numbers)) {
            $this->user_id = $userid;
            $this->text_sms = $text_sms;

            $text_sms_tmp = $this->text_sms;
            $url = $this->url_sms . '&mobilelist=' . $moble_numbers . '&content=' .
                urlencode($this->text_sms);
            $this->send_sms($moble_numbers);
        }
    }
    /**
     *向指定的号码发送短信
     * @param <type> $moble_numbers
     */
    public function send_sms($moble_numbers)
    {
        if (empty($moble_numbers) || $moble_numbers == "") {
            return;
        }
        $text_sms_tmp = $this->text_sms;
        $this->text_sms = iconv('utf-8', 'gbk', $this->text_sms);
        $url = $this->url_sms . '&mobilelist=' . $moble_numbers . '&content=' .
            urlencode($this->text_sms);
        $file_contents = '';
        ob_flush();
        flush();

        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);

        //发送日志
        $sql = <<< SQL
        insert into system_sms_send
        set mobile_num='$moble_numbers',text='$text_sms_tmp',url='$url_sms',create_by=$this->user_id
        ,status = 1,send_time_last=now(),send_time_first=now(),last_response='$file_contents'
SQL;
        $db2 = &$GLOBALS['db2'];
        $db2->query($sql);

    }

    public function send_sms_sixty($moble_numbers)
    {

        
        if (empty($moble_numbers) || $moble_numbers == "") {
            return;
        }
        $p = 60;
        
        $co = ceil(count($moble_numbers) / $p);
        
        $text_sms_tmp = $this->text_sms;
        $this->text_sms = iconv('utf-8', 'gbk', $this->text_sms);

        for ($x = 0; $x < $co; $x++) {
            $count=count($moble_numbers);
            $end=$x * $p + $p;
            
            if ( $count<=$end) {
                $to = count($moble_numbers);
            } else {
                $to = $x * $p + $p;
            }
            
            for ($i = $x * $p; $i < $to; $i++) {
                $mo[$x] = $mo[$x] . $moble_numbers[$i] . ',';
            }

            $url = $this->url_sms . '&mobilelist=' . $mo[$x] . '&content=' . urlencode($this->text_sms);
            $file_contents = '';
            //ob_flush();
            //flush();

            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $file_contents = curl_exec($ch);
            curl_close($ch);
            $moble_numbersx = $mo[$x];
            
            $sql = <<< SQL
        insert into system_sms_send
        set mobile_num='$moble_numbersx',text='$text_sms_tmp',url='$url_sms',create_by=$this->user_id
        ,status = 1,send_time_last=now(),send_time_first=now(),last_response='$file_contents' 
SQL;

            $db2 = &$GLOBALS['db2'];
            $db2->query($sql);
        }
    }

    /**
     *给指定用户发送短信
     * @param <type> $userid
     */
    public function sendByUserId($userid)
    {
        $num = $this->getPhoneNumberByUserId($userid);
        if (!empty($num) && $num != "")
            $this->send_sms($num);
    }

    /**
     *根据分组发送短信
     * @param <type> $group_id
     */
    public function sendByGroupId($group_id)
    {
        $num_list = $this->getNumberListByGroupId($group_id);

        $moble_numbers = "";

        if (!empty($num_list)) {
            for ($i = 0; $i < count($num_list); $i++) {
                $moble_numbers = $num_list[$i] . ",";
            }
            $this->send_sms($moble_numbers);
        }
    }

    /**
     *根据群组ID获取用户手机号码列表
     * @param <type> $group_id
     * @return <type>
     */
    private function getNumberListByGroupId($group_id)
    {
        $mobilelist;
        $sql_userIds = 'select u.mobile_number from users u inner join groups_followed gf on u.id=gf.user_id where group_id=' .
            $group_id;
        $db2 = &$GLOBALS['db2'];
        $db2->query($sql_userIds);
        while ($obj = $db2->fetch_object()) {
            if (empty($obj->mobile_number)) {
                continue;
            }
            $mobilelist[] = $obj->mobile_number;
        }

        return $mobilelist;
    }

    /**
     *根据用户Id获取手机号码
     * @param <type> $user_id
     * @return <type>
     */
    private function getPhoneNumberByUserId($user_id)
    {
        $sql = 'select u.mobile_number from users u  where u.id=' . $user_id;
        $db2 = &$GLOBALS['db2'];
        $db2->query($sql);
        while ($obj = $db2->fetch_object()) {
            if (empty($obj->mobile_number)) {
                return false;
            }
            return $obj->mobile_number;
        }
    }
}
?>